/**
 @Name：CLTPHP（Pro版） 管理员日志管理
 @Author：chichu
 @Site：http://www.cltphp.com/
 @License：CLTXY
 */
layui.define(['admin', 'table', 'util','upload'], function (exports) {
    var $ = layui.$, admin = layui.admin, table = layui.table, element = layui.element, form = layui.form,util = layui.util, upload = layui.upload;
    //全部数据
    tableIn = table.render({
        elem: '#list',
        url: '/shengteng/adminLog/index',
        toolbar: '#toolbar',
        method: 'post',
        autoSort: false,
        page:true,
        cols: [[
            {checkbox: true, fixed: true},
            {field: 'id', title: '编号', width: 80, fixed: true,sort:true},
            {field: 'username', title: '用户名', width: 100,edit:'text'},
            {field: 'title', title: '标题', width: 300,sort:true},
            {field: 'url',  title: 'url', width: 200, edit:'text',sort:true},
            {field: 'ip', align: 'center', title: 'IP', width: 100},
            {field: 'useragent', title: '浏览器',width: 100,sort:true},
            {field: 'create_time', title: '操作时间',width: 180,sort:true},
            {width: 100, align: 'center', toolbar: '#action'}
        ]]
    });
    //监听排序事件
    table.on('sort(list)', function(obj){
        table.reload('list', {
            initSort: obj
            ,where: {sort_by: obj.field,sort_order: obj.type}
        });
    });
    //监听工具条
    table.on('tool(list)', function (obj) {
        var data = obj.data;
        var id = data.id;
        if (obj.event === 'del') {
            layer.confirm('您确定要删除该记录吗？', function () {
                admin.req({
                    url: '/shengteng/adminLog/del/'
                    , type: 'post'
                    , data: {'id': id}
                    , success: function (res) {
                        layer.closeAll('dialog');
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        }
                    }
                });
            });
        }
    });

    //头工具栏事件
    table.on('toolbar(list)', function(obj){
        switch(obj.event){
            case 'direct':
                var checkStatus = table.checkStatus('list'),data = checkStatus.data; //获得选中的数据
                if(data.length === 0) return layer.msg('未选中行',{icon:2});
                var ids = [];
                $.each(data, function(i,val){
                    ids[i] = val['id'];
                });
                layer.confirm('确定删除所选记录吗？', function(){
                    admin.req({
                        url: '/shengteng/adminLog/del/'
                        , type: 'post'
                        , data: {id:ids}
                        , success: function (res) {
                            layer.closeAll('dialog');
                            if(res.code==0){
                                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                    tableIn.reload();
                                });
                            }
                        }
                    });
                });
                break;
            case 'direct_all':
                layer.confirm('确定删除全部记录吗？', function(){
                    admin.req({
                        url: '/shengteng/adminLog/del/'
                        , type: 'post'
                        , data: {id:''}
                        , success: function (res) {
                            layer.closeAll('dialog');
                            if(res.code==0){
                                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                    tableIn.reload();
                                });
                            }
                        }
                    });
                });
        }
    });





    var active = {
        reload: function () {
            var key = $('#key').val();
            var asId = $('#asId').val();
            //执行重载
            table.reload('list', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: {key: key,asId:asId}
            });
        }
    };
    $('.table-head-btn .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    exports('adminLog', {});
});
